﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using System.Text;
using Newtonsoft.Json;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Param
{
    /// <summary>
    /// 针对配置参数类型：
    /// 参数组扩展项为：[{"Code":"Name","Value":"名称","Type":"S"}]
    /// </summary>
    public partial class NormalParamList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        
        public string _groupId;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _groupId = Request["gid"] == null ? null : Request["gid"];

            //初始化控件值

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            if (_framePage.IsAjaxPagerPostBack)
            {
                //分页返回
                BindList(_framePage.PageIndex, _framePage.PageSize);
            }
            else
            {
                //第一次进入页面
                BindList(1, _framePage.PageSize);

            }
        }

        protected string GetTitle(object param5)
        {
            List<ParamItemExp> expItemList;

            if (param5 != null)
            {
                //可能没有配置扩展
                expItemList = JsonConvert.DeserializeObject<List<ParamItemExp>>(param5.ToString());

                if (expItemList != null)
                {
                    foreach (ParamItemExp itemExp in expItemList)
                    {
                        // 固定为 Name 的项
                        if (itemExp.Code == "Name")
                        {
                            return itemExp.Value;
                        }
                    }
                }
            }
            return "&nbsp;";
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<SysParam>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }

        protected int GetListCount()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<SysParam>();
                SetupCondition(ref criteria);
                return (int)criteria.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="crt"></param>
        protected void SetupCondition(ref ICriteria crt)
        {
            crt.Add(Expression.Eq("GroupCode", _groupId));
        }
    }
}
